package com.wwg.market.ui.code.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.mobileares.midp.widgets.client.button.ButtonBar;
import com.mobileares.midp.widgets.client.button.CButton;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.nexustar.gwt.widgets.client.ui.table.TableItem;
import com.wwfish.gwt.model.client.IFishModel;
import com.wwfish.gwt.model.client.impl.FishModelManager;
import com.wwfish.gwt.model.client.impl.ListModel;
import com.wwfish.gwt.model.client.impl.Listener;
import com.wwfish.gwt.model.client.impl.ListenerHandler;
import com.wwg.market.ui.code.client.widgets.CodeListWidget;
import com.wwg.market.ui.code.client.widgets.ExListWidget;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.dashboard.client.widgets.PageItem;
import com.wwg.market.ui.service.dto.GeneralCodeDto;
import com.wwg.market.ui.service.interfaces.CodeService;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * 代码大类列表（第一级代码表）
 *
 * @author gaoyang
 * @version $Id: $
 * @date 11-12-19 下午10:14
 */
public class CodePageItem extends PageItem {
    private ExListWidget fstLevelListWidget;
    private CodeListWidget sndLevelListWidget;

    @Override
    protected void processMain(SimplePanel mainPanel) {
        fstLevelListWidget = new ExListWidget();
        sndLevelListWidget = new CodeListWidget(this);

        HorizontalPanel codeEditPanel = new HorizontalPanel();
        codeEditPanel.setSpacing(1);
        codeEditPanel.add(fstLevelListWidget);
        codeEditPanel.add(sndLevelListWidget);
        mainPanel.setWidget(codeEditPanel);

//        fstLevelListWidget.setCreateChildCommand(new Command() {
//            public void execute() {
//                saveChildCategory(fstLevelListWidget, sndLevelListWidget, "创建代码");
//            }
//        });

        fstLevelListWidget.setChildWidget(sndLevelListWidget);

        fstLevelListWidget.setSelectionHandler(new SelectionHandler<TableItem>() {
            public void onSelection(SelectionEvent<TableItem> tableItemSelectionEvent) {
                final GeneralCodeDto parent = (GeneralCodeDto) tableItemSelectionEvent.getSelectedItem().getUserObject();
                if (parent.getChildren() == null) {
                    System.out.println(parent + " first loading ...");
                    Utils.invoke(CodeService.class.getName(), "getChildren", new Object[]{parent}, new LoadingAsyncCallback() {
                        @Override
                        protected void success(Object result) {
                            parent.setChildren((List) result);
                            sndLevelListWidget.getListWidget().getFishModel().load((List) result);
                            System.out.println(parent + " has loaded!");
                        }
                    });
                } else {
                    sndLevelListWidget.getListWidget().getFishModel().load(parent.getChildren());
                }
            }
        });
    }


    @Override
    protected void processMenu(ButtonBar bar) {
        CButton view = new CButton("创建类别");

        view.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                CodeFormFactory.getEditCodeForm().setModel(fstLevelListWidget.getListWidget().getFishModel());
                CodeFormFactory.getEditCodeForm().show(new GeneralCodeDto());
            }
        });
        bar.addButton(view);
    }

    @Override
    public void initModel() {
        fstLevelListWidget.getListWidget().setFishModel((ListModel<GeneralCodeDto>) FishModelManager.getAppModel("codes"));
        sndLevelListWidget.getListWidget().setFishModel(getTempListModel());

        fstLevelListWidget.getListWidget().getFishModel().getListenerManager().addListener(Listener._LOAD,
                new ListenerHandler() {
                    public void execute(IFishModel model) {
                        sndLevelListWidget.getListWidget().getFishModel().load(null);
                    }
                });
    }

//    public void saveChildCategory(ExListWidget parentWidget, CodeListWidget currentWidget, String title) {
//        GeneralCodeDto parent = (GeneralCodeDto) parentWidget.getListWidget().getBMUI().getSelectedItem().getUserObject();
//        CodeFormFactory.getEditCodeForm().setModel(currentWidget.getListWidget().getFishModel());
//        GeneralCodeDto dto = new GeneralCodeDto();
//        dto.setParent(parent.getId());
//        CodeFormFactory.getEditCodeForm().show(dto);
//    }

    public ListModel<GeneralCodeDto> getTempListModel() {
        ListModel<GeneralCodeDto> model = (ListModel<GeneralCodeDto>) FishModelManager.createTempModel(FishModelManager.TYPE_LIST);
        return model;
    }

    @Override
    public void ddOutModel(Object ob) {
        fstLevelListWidget.render();
    }

}
